Conversation
|
|
||
| ### d) Side effects and testing | ||
|
|
||
| Because `:=` modifies by reference, changes are cumulative. If the same *data.table* is reused—for example, in a loop or when using `test()` with multiple `optimization` levels—subsequent runs will start with the modified table from the previous run. Use `copy()` to ensure each run starts with the same data. |
| \code{DT[a > 4, b := c]} is different from \code{DT[a > 4][, b := c]}. The first expression updates (or adds) column \code{b} with the value \code{c} on those rows where \code{a > 4} evaluates to \code{TRUE}. \code{X} is updated \emph{by reference}, therefore no assignment needed. Note that this does not apply when \code{i} is missing, i.e. \code{DT[]}. | ||
|
|
||
| The second expression on the other hand updates a \emph{new} \code{data.table} that's returned by the subset operation. Since the subsetted data.table is ephemeral (it is not assigned to a symbol), the result would be lost; unless the result is assigned, for example, as follows: \code{ans <- DT[a > 4][, b := c]}. | ||
| Note that \samp{:=} modifications are cumulative. When reusing a \code{data.table} in loops or multi-level tests, use \code{\link{copy}} to ensure a fresh state. |
There was a problem hiding this comment.
I don't think cumulative is good word here. In-place changing it's input seems to be more precise.
|
|
||
| ### d) Side effects and testing | ||
|
|
||
| Because `:=` modifies by reference, changes are cumulative. If the same *data.table* is reused for example, in a loop or when using `test()` with multiple `optimization` levels, subsequent runs will start with the modified table from the previous run. Use `copy()` to ensure each run starts with the same data. |
There was a problem hiding this comment.
similar to Jan's comment earlier, cumulative isn't the most precise here
|
|
||
| } | ||
| \keyword{ data } | ||
|
|
There was a problem hiding this comment.
please stop removing those newlines...
|
|
||
| ### d) Side effects and testing | ||
|
|
||
| Because `:=` modifies by reference, changes to a data.table persist after evaluation. If the same *data.table* is reused for example, in a loop or when using `test()` with multiple `optimization` levels, subsequent runs will start with the modified table from the previous run. Use `copy()` to ensure each run starts with the same data. |
There was a problem hiding this comment.
If you already state the optimization levels then it would be good to also use this in the example.
There was a problem hiding this comment.
the different optimization levels are when either supply multiple values to test(optimize=) or via set them explicitly options(datatable.optimize=0L) or 1, 2, Inf, etc...
Its 100 % ok to ask questions, but please refrain from just doing something to have something done, because thats neither helping you nor us.
closes #7409
Documented
:=side effects in repeated execution acrossdatatable-reference-semantics.Rmd,assign.Rd, andtest.Rd.hi @ben-schwen, whenever you have a chance, could you please take a look at this PR.
thanks